package com.gy.spring.mvc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gy.spring.mvc.entity.User;
import com.gy.spring.mvc.service.RedisService;
import com.gy.spring.mvc.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * TODO 用户控制器
 *
 * @author geYang
 * @date 2018-04-16
 */
@RestController
@RequestMapping("user")
@Api(description = "用户相关API")
public class UserController {
    @Autowired
    private RedisService redisService;

    @Autowired
    private UserService userService;

    /**
     * TODO 用户列表
     *
     * @return
     * @author geYang
     * @date 2018-04-16 11:37
     */
    @ApiOperation(value = "用户列表", notes = "获取所有用户列表")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Object list() {
        String userList = redisService.get("user:list");
        if (userList == null) {
            userList = userService.selectList(null).toString();
            redisService.set("user:list", userList);
        }
        return userList;
    }

    /**
     * TODO 用户详情
     *
     * @param id
     * @return
     * @author geYang
     * @date 2018-04-16 11:37
     */
    @ApiOperation(value = "用户详情", notes = "获取用户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "int"),
    })
    @RequestMapping(value = "get/{id}", method = RequestMethod.GET)
    public Object get(@PathVariable("id") int id) {
        String userStr = redisService.get("user:" + id);
        if (userStr == null) {
            User user = userService.selectById(id);
            if (user != null) {
                userStr = user.toString();
                redisService.set("user:" + id, userStr);
            }
        }
        return userStr;
    }

    /**
     * TODO 用户添加
     *
     * @param user
     * @return
     * @author geYang
     * @date 2018-04-16 11:37
     */
    @ApiOperation(value = "用户添加", notes = "添加用户信息")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Object add(User user) {
        return userService.insert(user);
    }

    /**
     * TODO 用户更新
     *
     * @param user
     * @return
     * @author geYang
     * @date 2018-04-16 11:36
     */
    @ApiOperation(value = "用户更新", notes = "更新用户信息")
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public Object update(User user) {
        redisService.del("user:" + user.getId());
        return userService.updateById(user);
    }

    /**
     * TODO 用户删除
     *
     * @param id
     * @return
     * @author geYang
     * @date 2018-04-16 11:37
     */
    @ApiOperation(value = "用户删除", notes = "删除用户详情")
    @RequestMapping(value = "del/{id}", method = RequestMethod.DELETE)
    public Object del(
            @ApiParam(name = "id", value = "用户ID", required = true)
            @PathVariable("id") int id) {
        redisService.del("user:" + id);
        return userService.deleteById(id);
    }

    /**
     * TODO 回滚测试
     *
     * @throws Exception
     * @author geYang
     * @date 2018-04-16 11:38
     */
    @ApiOperation(value = "回滚测试", notes = "事务回滚测试")
    @RequestMapping(value = "test", method = RequestMethod.GET)
    public void test() throws Exception {
        userService.test();
    }

    /**
     * TODO 缓存测试
     *
     * @return
     * @author geYang
     * @date 2018-04-16 11:38
     */
    @ApiOperation(value = "缓存测试", notes = "MyBatis-ehcache缓存测试")
    @RequestMapping(value = "testCache", method = RequestMethod.GET)
    public Object testCache() {
        userService.selectById(1);
        userService.selectById(1);
        return userService.selectById(1);
    }

    @RequestMapping(value = "testCache2", method = RequestMethod.GET)
    public Object testCache2() {
        userService.listUser();
        userService.listUser();
        return userService.listUser();
    }

}
